﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using GoodMan.Infrastructure.Entities.Repository;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Data.Entity.Migrations;
using System.Data.Entity.Infrastructure;
using System.Web;
using System.Text.RegularExpressions;

namespace GoodMan.Infrastructure.Entities
{
    public class Context : DbContext
    {
        public Context() : this(GetContextName())  { }
        public Context(string contextName)
            : base(contextName)
        {
            Database.SetInitializer<Context>(null);
        }
        public DbSet<T_User> User { get; set; }
        public DbSet<T_Dormitory> Dormitory { get; set; }
        public DbSet<T_SpecialSale> SpecialSale { get; set; }
        public DbSet<T_Recommended> Recommended { get; set; }
        public DbSet<T_Duty> Duty { get; set; }
        public DbSet<T_CostRecord> CostRecord { get; set; }
        public DbSet<T_Recharge> Recharge { get; set; }
        public DbSet<T_Refund> Refund { get; set; }
        public DbSet<T_Integration> Integration { get; set; }
        public DbSet<T_Information> Information { get; set; }
        public DbSet<T_Order> Order { get; set; }
        public DbSet<T_OrderHistory> OrderHistory { get; set; }
        public DbSet<T_OrderDetail> OrderDetail { get; set; }
        public DbSet<T_Policy> Policy { get; set; }
        public DbSet<T_Business> Business { get; set; }
        public DbSet<T_Product> Product { get; set; }
        public DbSet<T_ProductCategory> ProductCategory { get; set; }
        public DbSet<T_ProductRepertory> ProductRepertory { get; set; }
        public DbSet<T_ProductRepertoryHistory> ProductRepertoryHistory { get; set; }
        public DbSet<T_Appraise> Appraise { get; set; }
        public DbSet<T_RebateReport> RebateReport { get; set; }
        public DbSet<T_Notice> Notice { get; set; }
       
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();//移除复数表名的契约
        }
        private static string GetContextName()
        {
            string host = HttpContext.Current.Request.Url.Host;
            string contextName = "name=Context";
            if(new Regex("([A-Za-z0-9\\-]+\\.){2}com").IsMatch(host))
            {
                if (host.Substring(0, host.IndexOf('.')) != "www")
                {
                    contextName = "name=" + host.Substring(0, host.IndexOf('.'));
                }
            }
            return contextName;
        }
    }
}
